package com.two.service.order.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.two.common.util.CommonResult;
import com.two.service.order.entity.Orders;
import com.two.service.order.feign.PingLunFeign;
import com.two.service.order.feign.TuiMoneyFeign;
import com.two.service.order.vo.*;
import com.two.service.order.orderservice.IOrdersService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private IOrdersService orderService;


    @Resource
    private PingLunFeign pingLunFeign;

    @Resource
    private TuiMoneyFeign tuiMoneyFeign;

    // 商家确认接单
    @PostMapping("/confirmOrders")
    public CommonResult confirmOrders(@RequestBody Orders[] orders ){
        return orderService.confirmOrders(orders);
    }

    // PC后端查询所有餐厅营业额
    @PostMapping("/findMoney")
    public CommonResult findMoney(String token){
      /*  System.out.println("===========");
        String token = request.getHeader("token");
        System.out.println("==========>"+token);*/
        //return  orderService.findMoney(token);
        return  orderService.findMoney(token);
    }

    // 手机端退款功能
    @PostMapping("/returnMoney")
    public CommonResult returnMoney (String reason,@RequestBody OrdersVo ordersVo){
        System.out.println("=====>ordersVo++>"+ordersVo);
        System.out.println("reson=====>"+reason);
        String oid = ordersVo.getOid();

        orderService.updateProductCode(oid);

        TuiMoneyVo tuiMoneyVo = new TuiMoneyVo();
        tuiMoneyVo.setUsermonicker(ordersVo.getUsermonicker());
        tuiMoneyVo.setDishName(ordersVo.getDishName());
        tuiMoneyVo.setTuiReason(reason);
        tuiMoneyVo.setRid(ordersVo.getRid());
        tuiMoneyVo.setTuiTime(LocalDateTime.now());
        tuiMoneyVo.setStatus(0);
        tuiMoneyVo.setAlipayNo(ordersVo.getAlipayNo());
        tuiMoneyVo.setDishTotalPrice(Double.valueOf(ordersVo.getDishTotalPrice()));
        tuiMoneyVo.setOrdersid(ordersVo.getOrdersid());

        return tuiMoneyFeign.tuiMoney(tuiMoneyVo);
    }

    //  PC端查看订单详情
    @GetMapping("/orderDetailsByRid")
    public CommonResult ordersDetails(HttpServletRequest request,String ordersid){
        String token = request.getHeader("token");
        return orderService.orderDetailsByRid(token,ordersid);
    }

    // 手机端取消订单
    @GetMapping("/cancel")
    public CommonResult cancel( String ordersid){
        System.out.println("====>"+ordersid);
        return  orderService.deleteByOrdersId (ordersid);
    }

    // 手机端获取订单信息
    @GetMapping("/findOrders")
    public CommonResult findOrders(HttpServletRequest request){
        String token = request.getHeader("token");

        return orderService.findOrders(token);
    }

    // 修改支付状态
    @PostMapping("/updatePayState")
    public CommonResult updatePayState(String traceNo, String status, String now){
        return orderService.updatePayState( traceNo,  status,  now);
    }

    // 修改订单状态
    @PostMapping("/updateState")
    public CommonResult updateState(String tradeNo, String status, String gmtPayment, String alipayTradeNo){
       return orderService.updateState(tradeNo,status,gmtPayment,alipayTradeNo);
    }

    // 根据支付宝流水号获取订单
    @GetMapping("/getByNo")
    public Orders getByNo(String traceNo){
        return orderService.getByNo(traceNo);
    }

   /* @GetMapping("/buy")
    private CommonResult buy(@PathVariable GoodsVo goodsVo){
        return orderService.buy(goodsVo);
    }*/

    // 发送评论
    @PostMapping("/sendComment")
    public CommonResult sendComment(@RequestBody PinglunVo pinglunVo){
        System.out.println("pinglunVo====>"+pinglunVo.toString());
        orderService.updateCommentStatus(pinglunVo);
        return pingLunFeign.pinglun(pinglunVo);
    }
    // 购物车下单支付
    @PostMapping("/shopcar/payMoney")
    public CommonResult payMoney(String totalPrice, @RequestBody ShopcarVo[] shopcar){
        return orderService.payMoney(totalPrice,shopcar);
    }

    // 手机端提交订单
    @PostMapping("/shopPay")
    public CommonResult shopPay(@RequestParam String totalPrice, @RequestBody RestautantVo[] restautantVo){
        System.out.println("订单页面的shopPay=====================");
        System.out.println("订单页面的shopPay====================="+restautantVo[0].toString());
        return orderService.shopPay(totalPrice,restautantVo);
    }

    // 手机端查看订单详情
    @GetMapping("/orderDetails")
    private CommonResult orderDetails(String ordersid){
        return orderService.orderDetails(ordersid);
    }

    // PC端根据条件查询已确认的订单并分页
    @PostMapping("/searchSuccessOrders")
    public CommonResult searchSuccessOrders(Integer currentPage, Integer pageSize, @RequestBody Orders orderVo, HttpServletRequest request){
        String token = request.getHeader("token");
        return orderService.searchSuccessOrders(currentPage,pageSize,orderVo,token);
    }

    // PC端根据条件查询未确认的订单并分页
    @PostMapping("/search")
    public CommonResult search(Integer currentPage, Integer pageSize, @RequestBody Orders orderVo, HttpServletRequest request){
        String token = request.getHeader("token");
        return orderService.search(currentPage,pageSize,orderVo,token);
    }

    // PC端删除订单
    @GetMapping("/delete")
    public CommonResult delete(String id){
        return  orderService.delete(id);
    }


    // 手机端确认订单页面查询订单 确认
    @PostMapping("/confirmPay")
    public CommonResult confirmPay(HttpServletRequest request){
        String token = request.getHeader("token");
        return orderService.confirmPay(token);
    }

    // 手机端确认收货
    @GetMapping("/recipt")
    public CommonResult recipt(String ordersid){
        return orderService.recipt(ordersid);
    }
}
